package com.zmn.plat.admin.controller.funswitch;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.dto.StatusDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.entity.funswitch.PublicBaseCodeMap;

/**
 * 公共基础配置管理编辑相关action处理
 * 
 * @author hedaiping
 * @create 2019/03/25/11:17
 */
@RequestMapping("/funswitch")
@Controller
public class FunctionModifyController extends FunctionBaseController {

    /**
     * 更新指定配置的状态
     * 
     * @param request
     * @param ids
     * @param status
     * @return
     */
    @RequiresPermissions("/funswitch/updateStatus.action")
    @RequestMapping("/updateStatus.action")
    @ResponseBody
    public ResultDTO updateCodeMapStatus(HttpServletRequest request, @RequestParam(value = "ids", defaultValue = "") String ids,
        @RequestParam(value = "status", defaultValue = "0") Integer status) {
        if (StringUtil.isBlank(ids)) {
            return new ResultDTO(StatusConsts.STATUS_ERROR, "ERROR", false, "请选择要更改状态的配置！");
        }
        if (status == 0) {
            return new ResultDTO(StatusConsts.STATUS_ERROR, "ERROR", false, "请设置配置状态！");
        }

        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);

        StatusDTO statusDTO = new StatusDTO();
        statusDTO.setUpdateTime(DateUtil.getNow());
        statusDTO.setUpdater(getMcStaffRealName());
        statusDTO.setStatus(status);
        statusDTO.setIds(arrayIds);
        baseCodeMapService.updateStatusBatch(statusDTO);
        return new ResultDTO(StatusConsts.STATUS_SUCCESS, "OK", true, GlobalConsts.SUCCESS_MSG);
    }

    /**
     * region 新增/修改 mapId为0表示新创建
     * 
     * @param request
     * @param codeMap
     * @return
     */
    @SuppressWarnings("Duplicates")
    @RequiresPermissions("/funswitch/addEdit.action")
    @RequestMapping("/addEdit.action")
    @ResponseBody
    public ResultDTO addOrEditCodeMap(HttpServletRequest request, @RequestBody PublicBaseCodeMap codeMap) {

        Integer mapId = codeMap.getMapId();
        try {
            Date now = DateUtil.getNow();
            if (mapId == null || mapId == 0) {
                codeMap.setCreater(getMcStaffRealName());
                codeMap.setCreateTime(now);
                baseCodeMapService.insert(codeMap);
            } else {
                codeMap.setUpdater(getMcStaffRealName());
                codeMap.setUpdateTime(now);
                baseCodeMapService.updateByKey(codeMap);
            }

            return new ResultDTO(StatusConsts.STATUS_SUCCESS, Integer.toString(codeMap.getMapId()), "OK", true, "操作成功！");
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("基础配置【{}】已经存在，不能重复添加！", codeMap.getName());
            return new ResultDTO(StatusConsts.STATUS_ERROR, "OK", true, "基础配置已经存在，不能重复添加！");
        } catch (Exception ex) {
            logger.error(String.format("[zmn]新增/修改配置失败,area:[%s],ex:[%s]", JSONObject.toJSONString(codeMap), ex.getMessage()));
            return new ResultDTO(StatusConsts.STATUS_ERROR, "OK", true, "操作失败！");
        }

    }
}
